package com.conduit.comnands;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.conduit.WinReg.WindowsReqistry;
import com.conduit.enums.ResultCode;
	
public class GetRegistryValueCommand extends Command {
	
	public GetRegistryValueCommand() {
		commandSyntax = "get registry value\\s{2,}(.*)\\s{2,}(.*)\\s{2,}(.*)}";
	}

	@Override
	public void executeWithArgs(String args){
		// validate command syntax
		if (!args.matches(commandSyntax)) {
			setResult("Error in Command's syntax (\"" + args + "\")");
			setResultCode(ResultCode.Error);
			return;
		}
		// get commnad's arguments
		Matcher matcher = Pattern.compile(commandSyntax).matcher(args);
		if (matcher.find()) {
			String regKey = matcher.group(1).trim();
			String regValue = matcher.group(2).trim();
			String saveAsArgument = matcher.group(3).trim();
			// get expected value from command
			// get actual result
			try {
				String actualRegistryValue = WindowsReqistry.readRegistry(regKey, "\"" + regValue + "\"");
				setResult(saveAsArgument + " is a " + actualRegistryValue);
				setReturnValue(true);
				setResultCode(ResultCode.Successes);
				// if registry query did not find any value or other error in
				// query
			} catch (Exception e) {
				setResult(e.getMessage());
				setResultCode(ResultCode.Error);
			}
		}
	}

	@Override
	public String getCommandName() {
		return "Get Registry Value";
	}

}
